<template>
  <div class="app-container calendar-list-container">
    <!-- 搜索栏 -->
    <div class="filter-container">
      <el-input style="width: 200px;" class="filter-item" placeholder="车次的名称" v-model="listQuery.name">
      </el-input>
      <el-date-picker v-model="listQuery.startTime" type="datetime" placeholder="开始时间"></el-date-picker>  
      <el-date-picker v-model="listQuery.endTime" type="datetime" placeholder="结束时间"></el-date-picker>  
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">新增</el-button>
      <el-button type="file" icon="el-icon-upload el-icon--right" @click="handleUpload">导入</el-button>
    </div>
    <!-- 搜索栏 End -->

    <!-- 列车信息数据表格 -->
    <el-table :data="list" v-loading="listLoading" element-loading-text="給我一點時間" border fit highlight-current-row style="width:100%">
      <el-table-column width="50" type="index"></el-table-column>
      <el-table-column align="center" label="到达车次" width="200">
        <template slot-scope="scope">
          <span>
            {{scope.row.arriveName}}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="到达时间">
        <template slot-scope="scope">
          <span>{{scope.row.arriveDate}} </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="轨道">
        <template slot-scope="scope">
          <span>{{scope.row.trackNum}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="出发车次" width="200">
        <template slot-scope="scope">
          <span>
            {{scope.row.outSetName}}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="出发时间">
        <template slot-scope="scope">
          <span>{{scope.row.outSetDate}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <span>{{scope.row.status}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" round size="mini" @click="handleUpdate(scope.row.id)">编辑</el-button>
          <el-button type="danger" round size="mini" @click="handleDelete(scope.row.id,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.pageIndex" :page-sizes="[10,20,30, 50]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 列车信息表格 End-->

    <!--上传对话框-->
    <el-dialog title="上传" :visible.sync="uploadVisible" width="300px">
      <el-input type="file" id="fileElem" style="display:none" @change="showFileName" accept=".xls , .xlsx"></el-input>
      <el-button type="primary" @click="selectFile">点击上传</el-button>
      <span id="fileName">{{this.fileName}}</span>
      <div slot="footer" class="dialog-footer">
        <el-button @click="uploadVisible = false">取消</el-button>
        <el-button type="primary" @click="uploadData()">确定</el-button>
      </div>
    </el-dialog>
    <!--上传对话框 End-->

    <!--对话框-->
    <el-dialog title="基础信息" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="trainInfo" :rules="rules" label-position="right" label-width="100px" ref="trainInfoForm">
        <el-form-item label="到达车次" prop="arriveName">
          <el-input type="text" v-model="trainInfo.arriveName"> </el-input>
        </el-form-item>

        <el-form-item label="到达时间" prop="arriveDate">
          <el-date-picker type="datetime" v-model="trainInfo.arriveDate"></el-date-picker>
        </el-form-item>

        <el-form-item label="轨道" prop="trackNum">
          <el-input-number type="number" v-model.number="trainInfo.trackNum" :min="1" :max="13"></el-input-number>
        </el-form-item>

        <el-form-item label="出发时间" prop="outSetDate">
          <el-date-picker type="datetime" v-model="trainInfo.outSetDate"></el-date-picker>
        </el-form-item>

        <el-form-item label="出发车次" prop="outSetName">
          <el-input v-model="trainInfo.outSetName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">保存</el-button>
      </div>
    </el-dialog>
    <!--对话框 End-->
  </div>
</template>
<script>
import {
  importBaseInfo,
  fetchList,
  createOrUpdate,
  getById,
  deleteById
} from '@/api/base'
import uploadComponents from '@/components/UploadExcel'
export default {
  data() {
    return {
      tablekey: 0,
      list: null,
      total: null,
      listLoading: false,
      fileName: '',
      listQuery: {
        pageIndex: 1,
        pageSize: 20,
        name: undefined,
        startTime: undefined,
        endTime: undefined
      },
      trainInfo: {
        id: undefined,
        arriveName: '',
        outSetName: '',
        arriveDate: new Date(),
        outSetDate: new Date(),
        trackNum: 1
      },

      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogFormVisible: false, // 是否显示编辑或者新建对话框
      uploadVisible: false, // 是否显示上传对话框
      rules: {
        arriveName: [
          { required: true, message: '到达车次必填', trigger: 'blur' }
        ],
        arriveDate: [
          {
            type: 'date',
            required: true,
            message: '到达时间必填',
            trigger: 'change'
          }
        ],
        outSetDate: [
          {
            type: 'date',
            required: true,
            message: '出发时间必填',
            trigger: 'change'
          }
        ],
        outSetName: [
          { required: true, message: '出发车次必填', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    resetTrainInfo() {
      this.trainInfo = {
        id: undefined,
        arriveName: '',
        outSetName: '',
        arriveDate: new Date(),
        outSetDate: new Date(),
        trackNum: 1
      }
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.list
        this.total = response.count
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageIndex = 1
      this.getList()
    },
    handleCreate() {
      // 新添加
      this.resetTrainInfo()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    createData() {
      this.$refs['trainInfoForm'].validate(vaild => {
        if (vaild) {
          createOrUpdate(this.trainInfo).then(response => {
            this.dialogFormVisible = false
            this.$message({
              message: '恭喜你，新添加成功',
              type: 'success'
            })
          })
          this.listQuery.pageIndex = 1
          this.getList()
        }
      })
    },
    handleUpdate(id) {
      this.dialogStatus = 'edit'
      this.dialogFormVisible = true
      getById(id).then(response => {
        this.trainInfo = response.trainInfo
      })
    },
    updateData() {
      this.$refs['trainInfoForm'].validate(vaild => {
        if (vaild) {
          createOrUpdate(this.trainInfo).then(response => {
            this.dialogFormVisible = false
            this.$message({
              message: '恭喜你，更新成功！',
              type: 'success'
            })
          })
          this.listQuery.pageIndex = 1
          this.getList()
        }
      })
    },
    handleDelete(id, row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteById(id).then(response => {
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleUpload() {
      this.uploadVisible = true
    },
    selectFile() {
      document.getElementById('fileElem').click()
    },
    showFileName() {
      debugger
      var fileConten = document.getElementById('fileElem').files
      if (fileConten != null && fileConten.length === 1) {
        this.fileName = fileConten[0].name
      }
    },
    uploadData() {
      var fileConten = document.getElementById('fileElem').files
      if (fileConten != null && fileConten.length === 1) {
        importBaseInfo(fileConten[0]).then(response => {
          if (response.statue === 'success') {
            this.$message({
              type: 'success',
              message: '导入成功!'
            })
          }
        })
      } else if (fileConten != null && fileConten.length === 0) {
        this.$message({
          type: 'error',
          message: '请选择导入的文件！'
        })
      }
    },
    handleCurrentChange(val) {
      this.listQuery.pageIndex = val
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getList()
    }
  }
}
</script>

<style lang="css" scoped>
.filter-container {
  padding-bottom: 10px;
}
.app-container {
  padding: 20px;
}
.pagination-container {
  margin-top: 30px;
}
</style>
